package cn.com.blueInfo.bpm.person.service.impl;

import cn.com.blueInfo.bpm.person.convert.OrganizeLevelConvert;
import cn.com.blueInfo.bpm.person.entity.OrganizeLevel;
import cn.com.blueInfo.bpm.person.repository.OrganizeLevelRepository;
import cn.com.blueInfo.bpm.person.request.OrganizeLevelDTO;
import cn.com.blueInfo.bpm.person.response.OrganizeLevelVO;
import cn.com.blueInfo.bpm.person.service.OrganizeLevelService;
import cn.com.blueInfo.core.util.SnowflakeIdGenerator;
import cn.com.blueInfo.utils.result.ResultInfo;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 组织级别表 业务服务实现类
 * @author suxch
 * @since 2025-09-01 19:28:09
 */
@Log4j2
@Service
public class OrganizeLevelServiceImpl implements OrganizeLevelService {

    @Autowired
    private OrganizeLevelRepository organizeLevelRepository;

    @Autowired
    private SnowflakeIdGenerator snowflakeIdGenerator;

    @Autowired
    private OrganizeLevelConvert organizeLevelConvert;

    /**
     * 新增组织级别
     * @param organizeLevelDTO 组织级别参数
     */
    @Override
    public ResultInfo<Integer> addOrganizeLevel(OrganizeLevelDTO organizeLevelDTO) {
        OrganizeLevel organizeLevel = organizeLevelConvert.dtoToEntity(organizeLevelDTO);
        organizeLevel.setId(snowflakeIdGenerator.nextId());
        Integer addSize = organizeLevelRepository.createOrganizeLevel(organizeLevel);
        return ResultInfo.<Integer>builder().data(addSize).message("组织级别成功新增 {} 条").build();
    }

    /**
     * 批量新增组织级别
     * @param organizeLevelDTOList 组织级别参数列表
     */
    @Override
    public ResultInfo<Integer> batchAddOrganizeLevel(List<OrganizeLevelDTO> organizeLevelDTOList) {
        List<OrganizeLevel> organizeLevelList = organizeLevelConvert.dtoToEntity(organizeLevelDTOList);
        for (OrganizeLevel organizeLevel : organizeLevelList) organizeLevel.setId(snowflakeIdGenerator.nextId());
        Integer addSize = organizeLevelRepository.batchCreateOrganizeLevel(organizeLevelList);
        return ResultInfo.<Integer>builder().data(addSize).message("组织级别成功新增 {} 条").build();
    }

    /**
     * 删除组织级别
     * @param organizeLevelId 组织级别ID
     */
    @Override
    public ResultInfo<Integer> removeOrganizeLevel(Long organizeLevelId) {
        Integer removeSize = organizeLevelRepository.deleteOrganizeLevel(organizeLevelId);
        return ResultInfo.<Integer>builder().data(removeSize).message("组织级别成功删除 {} 条").build();
    }

    /**
     * 批量删除组织级别
     * @param organizeLevelIdList 组织级别ID列表
     */
    @Override
    public ResultInfo<Integer> batchRemoveOrganizeLevel(List<Long> organizeLevelIdList) {
        Integer removeSize = organizeLevelRepository.batchDeleteOrganizeLevel(organizeLevelIdList);
        return ResultInfo.<Integer>builder().data(removeSize).message("组织级别成功删除 {} 条").build();
    }

    /**
     * 更新部分组织级别
     * @param organizeLevelUpdatePartialDTO 组织级别参数
     */
    @Override
    public ResultInfo<Integer> modifyOrganizeLevelPartial(
            OrganizeLevelDTO.UpdatePartialDTO organizeLevelUpdatePartialDTO) {
        OrganizeLevel organizeLevel = organizeLevelConvert.dtoToEntity(organizeLevelUpdatePartialDTO);
        Integer modifySize = organizeLevelRepository.updateOrganizeLevelPartial(organizeLevel);
        return ResultInfo.<Integer>builder().data(modifySize).message("组织级别成功更新 {} 条").build();
    }

    /**
     * 更新全部组织级别
     * @param organizeLevelDTO 组织级别参数
     */
    @Override
    public ResultInfo<Integer> modifyOrganizeLevel(OrganizeLevelDTO organizeLevelDTO) {
        OrganizeLevel organizeLevel = organizeLevelConvert.dtoToEntity(organizeLevelDTO);
        Integer modifySize = organizeLevelRepository.updateOrganizeLevel(organizeLevel);
        return ResultInfo.<Integer>builder().data(modifySize).message("组织级别成功更新 {} 条").build();
    }

    /**
     * 查询一条组织级别
     * @param organizeLevelId 组织级别ID
     */
    @Override
    public ResultInfo<OrganizeLevelVO> queryOrganizeLevelById(Long organizeLevelId) {
        OrganizeLevel organizeLevel = organizeLevelRepository.getOrganizeLevelById(organizeLevelId);
        OrganizeLevelVO organizeLevelVO = organizeLevelConvert.entityToVO(organizeLevel);
        return ResultInfo.<OrganizeLevelVO>builder().data(organizeLevelVO).message("组织级别查询成功").build();
    }

    /**
     * 分页查询组织级别
     * @param organizeLevelQueryDTO 组织级别参数
     */
    @Override
    public ResultInfo<Page<OrganizeLevelVO>> queryOrganizeLevelPage(OrganizeLevelDTO.QueryDTO organizeLevelQueryDTO) {
        Page<OrganizeLevel> organizeLevelPage = organizeLevelRepository.getOrganizeLevelPage(organizeLevelQueryDTO);
        Page<OrganizeLevelVO> resultData = organizeLevelConvert.convertPage(organizeLevelPage);
        return ResultInfo.<Page<OrganizeLevelVO>>builder()
                .data(resultData)
                .message("组织级别分页查询成功，共 {} 条", resultData.getRecords().size())
                .build();
    }

    /**
     * 根据条件查询 组织级别 数量
     * @param organizeLevelQueryDTO 组织级别参数
     */
    @Override
    public ResultInfo<Long> queryOrganizeLevelCount(OrganizeLevelDTO.QueryDTO organizeLevelQueryDTO) {
        Long resultCount = organizeLevelRepository.getOrganizeLevelCount(organizeLevelQueryDTO);
        return ResultInfo.<Long>builder()
                .data(resultCount)
                .message("组织级别数量查询成功，共 {} 条", resultCount)
                .build();
    }

    /**
     * 根据条件查询 组织级别
     * @param organizeLevelQueryDTO 组织级别参数
     */
    @Override
    public ResultInfo<List<OrganizeLevelVO>> queryOrganizeLevelList(OrganizeLevelDTO.QueryDTO organizeLevelQueryDTO) {
        List<OrganizeLevel> organizeLevelList = organizeLevelRepository.getOrganizeLevelList(organizeLevelQueryDTO);
        List<OrganizeLevelVO> resultData = organizeLevelConvert.entityListToVoList(organizeLevelList);
        return ResultInfo.<List<OrganizeLevelVO>>builder()
                .data(resultData)
                .message("组织级别分页查询成功，共 {} 条", resultData.size())
                .build();
    }
}
